package com.kanshu.ksgb.fastread.doudou.module.download.retrofit;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.module.download.bean.DownLoadChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.download.bean.MakeMoneyBean;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;

/**
 * 作者    wcy
 * 时间    2018/12/6 9:07
 * 文件    doudou
 * 描述
 */
public class DownloadChapterPresenter extends BaseMvpPresenter {
    public DownloadChapterPresenter(Subject lifeCyclerSubject) {
        super(lifeCyclerSubject);
    }
    /**
     * 获取该书籍所有章节内容
     * @param bookId
     * @param callback
     */
    public void getDownloadChapter(String bookId, INetCommCallback<List<DownLoadChapterBean>> callback) {
        RetrofitHelper.getInstance().createService(DownloadChapterService.class).getDownloadChapter(bookId)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<DownLoadChapterBean>>(){
                    @Override
                    public void onResponse(BaseResult<List<DownLoadChapterBean>> listBaseResult, List<DownLoadChapterBean> chapterListBean, Disposable disposable) {
                        callback.onResponse(chapterListBean);
                    }
                    @Override
                    public void onError(int code, String errDesc) {
                        super.onError(code, errDesc);
                        callback.onError(code, errDesc);
                    }
                });
    }

    /**
     * 扣钱逻辑
     * @param bookId
     * @param chapter_ids
     * @param callback
     */
    public void downloadMake(String bookId, String chapter_ids, INetCommCallback<BaseResult<MakeMoneyBean>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "网络错误/网络无法连接");
            return;
        }
        RetrofitHelper.getInstance().createService(DownloadChapterService.class).downloadMake(bookId, chapter_ids)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<MakeMoneyBean>() {
                    @Override
                    public void onResponse(BaseResult<MakeMoneyBean> moneyBeanBaseResult, MakeMoneyBean moneyBean, Disposable disposable) {
                        callback.onResponse(moneyBeanBaseResult);
                    }
                    @Override
                    public void onError(int code, String errDesc) {
                        super.onError(code, errDesc);
                        callback.onError(400, errDesc + "");
                    }
                });
    }

}
